#include "mainuserview.h"
#include <QMessageBox>
#include "qdebug.h"

MainUserView::MainUserView(QObject *parent) :
    QObject(parent)
{
    this->mainView = new MainView();
    controller = new UserController();

    mainView->show();
    this->wellcome();
}

void MainUserView::wellcome()
{
    wellcomeFrame = new WellcomeView();
    mainView->setFrame(wellcomeFrame);
    QObject::connect(wellcomeFrame, SIGNAL(authenticate(QString,QString)),
                     this, SLOT(login(QString,QString)));
}

void MainUserView::login(QString registration, QString password)
{
    if(controller->singUp(registration, password))
    {
        this->systemUser = new UserModel();
        this->systemUser->setRegistration(registration);

        QMessageBox::information(this->mainView,
                                 QString::fromUtf8("Acesso"),
                                 QString::fromUtf8("Autorizado"));
        this->options();
    } else
    {        
        QMessageBox::critical(this->mainView, QString::fromUtf8("Acesso"),
                              QString::fromUtf8("NEGADO - Matrícula Invalida ou Senha Incorreta"));
    }
}

void MainUserView::options()
{
    userOptions = new OptionsView();

    frameInitial();

    mainView->setGroupBox(this->userOptions);
    QObject::connect(userOptions, SIGNAL(changePass(bool)),
                     this, SLOT(changePassWord()));
    QObject::connect(userOptions, SIGNAL(create(bool)),
                     this, SLOT(createUser()));
}

void MainUserView::frameInitial()
{
    emptyFrame = new EmptyFrame();
    mainView->setFrame(emptyFrame);
}


void MainUserView::createUser()
{
    createView = new CreateView();
    mainView->setFrame(createView);
    QObject::connect(createView, SIGNAL(create(QString,QString,QString,QString,QString)),
                     this, SLOT(slotCreatUser(QString,QString,QString,QString,QString)));
}


void MainUserView::slotCreatUser(QString name, QString type, QString description,
                                 QString registration, QString password)
{
    if(!controller->create(name, registration, type, description, registration, password))
    {
        QMessageBox::critical(this->createView, QString::fromUtf8("ERRO"),
                              QString::fromUtf8("ERRO na operação, nome ou matrícula já cadastrada"));
    }else
    {
        QMessageBox::information(this->createView, QString::fromUtf8("Sucesso"),
                                 QString::fromUtf8("Operação realizada com Sucesso!"));
    }
    createView->clearFields();
}


void MainUserView::changePassWord()
{
    changePass = new ChangePass();
    mainView->setFrame(changePass);
    QObject::connect(changePass,
                     SIGNAL(validateChange(QString,QString,QString)),
                     this, SLOT(slotChangePass(QString,QString,QString)));
}


void MainUserView::slotChangePass(QString registration, QString oldPassword, QString newPassword)
{   
    if(controller->authenticate(oldPassword)){
        if(controller->changePass(registration, newPassword))
        {
            QMessageBox::information(this->changePass, QString::fromUtf8("Sucesso"),
                                     QString::fromUtf8("Operação realizada com Sucesso!"));
        } else
        {
            QMessageBox::critical(this->changePass, QString::fromUtf8("ERRO"),
                                     QString::fromUtf8("ERRO na operação, entre em contato com o suporte"));
        }

    } else
    {
        QMessageBox::critical(this->changePass, QString::fromUtf8("ERRO"),
                                 QString::fromUtf8("Senha Atual não confere!"));
    }

    frameInitial();

}


MainUserView::~MainUserView(){
}
